package com.fy56.platform.logistics.service.impl;

import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.common.ResultStatus;
import com.fy56.platform.logistics.mapper.ProjectMapper;
import com.fy56.platform.logistics.model.Project;
import com.fy56.platform.logistics.service.web.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 项目Service实现
 * @author li
 * @date 2018-02-08 16:58
 */
@Service
public class ProjectServiceImpl implements IProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public List<Project> select(Project entity) {
        return projectMapper.select(entity);
    }

    @Override
    public Project selectByPrimaryKey(Long id) {
        return projectMapper.selectByPrimaryKey(id);
    }

    @Override
    public Project get(Project entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel insert(Project entity) {
        ResultModel result = null;
        int r = projectMapper.insert(entity);
        if(r == 0) {
            result = ResultModel.error(ResultStatus.INSERT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    @Transactional(readOnly = true)
    public ResultModel insertSelective(Project entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel update(Project entity) {
        ResultModel result = null;
        int r = projectMapper.updateByPrimaryKey(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel updateSelective(Project entity) {
        ResultModel result = null;
        int r = projectMapper.updateByPrimaryKeySelective(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel delete(Long id) {
        int r = projectMapper.deleteByPrimaryKey(id);
        if (r == 0){
            return new ResultModel(ResultStatus.DELETE_ERR);
        }
        return ResultModel.ok();
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel delete(Project entity) {
        int r = projectMapper.updateByPrimaryKeySelective(entity);
        if (r == 0){
            return new ResultModel(ResultStatus.DELETE_ERR);
        }
        return ResultModel.ok();
    }

    @Override
    public List<Project> findList(Project entity) {
        List<Project> list = projectMapper.findList(entity);
        return list;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel batchDelete(ArrayList<Project> list) {
        LinkedList<String[]> failedIds = new LinkedList<String[]>();
        int r = 0;
        for (Project model : list) {
            r = projectMapper.updateByPrimaryKeySelective(model);
            if (r == 0) {
                failedIds.add(new String[]{model.getId().toString(), "批量删除失败!"});
            }
        }
        if(failedIds.size() > 0){
            return new ResultModel(0, "", failedIds);
        }else{
            return ResultModel.ok();
        }
    }
}
